package top.kaoshanji.r1ch06a;

import java.beans.PropertyEditorSupport;

/**
 * 代码清单-r1ch06a-190页c：自定义一个属性编辑器
 * @author kaoshanji
 * ^_^
 * create time 2025/5/6 18:58
 */
public class CustomCarEditor extends PropertyEditorSupport {

    @Override
    public void setAsText(String text) throws IllegalArgumentException {
        if(text == null || text.indexOf(",") == -1){
            throw new IllegalArgumentException("设置的字符串格式不正确");
        }

        String[] infos = text.split(",");
        Car car = new Car();
        car.setBrand(infos[0]);
        car.setMaxSpeed(Integer.parseInt(infos[1]));
        car.setPrice(Double.parseDouble(infos[2]));

        setValue(car);
    }

    @Override
    public String getAsText() {
        Object value = getValue();
        if (value == null) {
            return "";
        } else {
            Car car = (Car)value;
            return car.getBrand()+","+car.getMaxSpeed()+","+car.getPrice();
        }

    }
}
